﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MyStyle.Model;
using MyStyle.Repositry;

namespace MyStyle.RepositoryLayer
{
    public class ApplicationDbContext: DbContext
    {
        public ApplicationDbContext(DbContextOptions options) : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new CustomerMap());
            modelBuilder.ApplyConfiguration(new CustomItemMap());
            // base.OnModelCreating(modelBuilder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            #region 打印SQL语句
            optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder =>
            {
                builder.AddConsole();
                builder.AddEventLog();
                builder.AddDebug();
            }));

            // 全局启用拆分查询
            //optionsBuilder
            //    .UseSqlServer(
            //        @"Server=(localdb)\mssqllocaldb;Database=EFQuerying;Trusted_Connection=True",
            //        o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
            #endregion
        }

        public DbSet<Customer> Customer { get; set; }
        public DbSet<CustomItem> CustomItem { get; set; }
    }
}
